import { defineConfig,loadEnv } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vue from '@vitejs/plugin-vue'
import {resolve} from 'path'
import pxToVw from "postcss-px-to-viewport"
import autoprefixer from "autoprefixer"
const Timestamp = new Date().getTime();//随机时间戳
// https://vitejs.dev/config/
export default defineConfig(({mode})=>{
  const config = loadEnv(mode, './')
  const server = {
    port: 8000,
    open: true,
    proxy:{}
  }

  server.proxy[config.VITE_API_URL]={
    target: config.VITE_BASE_URL,
    secure: false,
    changeOrigin: true,
    rewrite: (path) => path.replace(config.VITE_API_URL, "")
  }
  return {
    plugins: [
        vue(),
        AutoImport({
            resolvers: [ElementPlusResolver()],
        }),
        Components({
            resolvers: [ElementPlusResolver()],
        }),
    ],
    resolve: {
      alias: {
        "@": resolve(__dirname, "./src"),
      },
    },
    server,
    css: {
      preprocessorOptions:{
        scss:{
          api:'modern-compiler'
        }
      },
      postcss: {
        plugins: [
          autoprefixer({
            overrideBrowserslist: ['Android 4.1', 'iOS 7.1', 'Chrome > 31', 'ff > 31', 'ie >= 8']
          }),
          pxToVw({
            rootValue: '12px',
            unitToConvert: 'px', // 要转化的单位
            viewportWidth: 750, //100vw=750px，UI设计稿的宽度,vant是375。可参考这个：https://juejin.cn/post/6961737808339795975
            unitPrecision: 6, // 转换后的精度，即小数点位数
            propList: ['*'], // 指定转换的css属性的单位，*代表全部css属性的单位都进行转换
            viewportUnit: 'vw', // 指定需要转换成的视窗单位，默认vw
            fontViewportUnit: 'vw', // 指定字体需要转换成的视窗单位，默认vw
            selectorBlackList: ['ig-'], // 指定不转换为视窗单位的类名，
            minPixelValue: 1, // 默认值1，小于或等于1px则不进行转换
            mediaQuery: true, // 是否在媒体查询的css代码中也进行转换，默认false
            replace: true, // 是否转换后直接更换属性值
            landscape: false, // 是否处理横屏情况
            exclude: /node_modules/i  //要忽略并保留为px的文件路径
          })
        ],
      },
    },
    build: {
      // sourcemap: false, // 输出.map文件,默认是false
      rollupOptions: {
        output: {
          chunkFileNames: `static/js/[name].[hash]${Timestamp}.js`,
          entryFileNames: `static/js/[name].[hash]${Timestamp}.js`,
          assetFileNames: `static/[ext]/[name].[hash]${Timestamp}.[ext]`,
        },
      }
    },
  }
})
